package cn.cdeden.knowledge.controller.share;


import cn.cdeden.common.core.domain.R;
import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.knowledge.domain.FilesShare;
import cn.cdeden.knowledge.domain.dto.FilesShareDto;
import cn.cdeden.knowledge.service.FilesShareService;
import io.undertow.util.BadRequestException;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 *详情界面
 */
@RestController
@RequiredArgsConstructor
//@Api(tags = "链接分享管理")
@RequestMapping("/api/filesShare")
public class FilesShareController {


    private final FilesShareService filesShareService;


    /**
     * 分享链接:通过文件id查询多个分享链接
     * @param fileId
     * @param type
     * @param teamId
     * @param pageable
     * @return
     */
    @GetMapping("/file")
//    @Log("分享链接:通过文件id查询多个分享链接")
//    @ApiOperation("分享链接:通过文件id查询多个分享链接")
    public R<TableDataInfo<FilesShareDto>> queryByFileId(@RequestParam(value = "fileId") String fileId ,
                           @RequestParam(value = "type") String type,
                           @RequestParam(value = "teamId",required = false) String teamId,
                           Pageable pageable) throws BadRequestException {

        return R.ok(filesShareService.queryByFileId(fileId, type,teamId, pageable));
    }


    /**
     * 分享链接:通过id查询对应的链接
     * @param id
     * @return
     */
    @GetMapping("/id")
//    @Log("分享链接:通过id查询对应的链接")
//    @ApiOperation("分享链接:通过id查询对应的链接")
    public ResponseEntity queryById(@RequestParam Long id) {
        filesShareService.findById(id);
        return new ResponseEntity<>(filesShareService.findById(id), HttpStatus.OK);
    }


    /**
     * 分享链接:修改链接分享
     * @param resources
     * @return
     */
    @PutMapping
//    @Log("分享链接:修改链接分享")
//    @ApiOperation("分享链接:修改链接分享")
    public R<FilesShare> update(@Validated @RequestBody FilesShareDto resources) {
        return R.ok(filesShareService.updateById(resources));
    }


    /**
     * 分享链接:删除链接分享
     * @param ids
     * @return
     */
    @DeleteMapping
//    @Log("分享链接:删除链接分享")
//    @ApiOperation("分享链接:删除链接分享")
    public R delete(@RequestBody Set<Long> ids) {
        filesShareService.removeByIds(ids);
        return R.ok();
    }


    /**
     * 分享链接:通过分享人id查询我的分享
     * @param userId
     * @param type
     * @param teamId
     * @param name
     * @param pageable
     * @return
     */
    @GetMapping("/sharePerson")
//    @Log("分享链接:通过分享人id查询我的分享")
//    @ApiOperation("分享链接:通过分享人id查询我的分享")
    public ResponseEntity queryBySPerson(@RequestParam(value = "userId",required = false) String userId,
                                         @RequestParam(value = "type") String type,
                                         @RequestParam(value = "teamId",required = false) String teamId,
                                         @RequestParam(value = "name",required = false) String name,
                                         Pageable pageable) {
//        filesShareService.queryBySPerson(userId,type,teamId,name, pageable)
        return new ResponseEntity<>(filesShareService.queryBySPerson(userId,type,teamId,name, pageable), HttpStatus.OK);
    }


    /**
     * 分享链接:新增链接分享
     * @param resources
     * @return
     */
    @PostMapping
//    @Log("分享链接:新增链接分享")
//    @ApiOperation("分享链接:新增链接分享")
    public ResponseEntity create(@Validated @RequestBody FilesShareDto resources) throws BadRequestException {
        return new ResponseEntity<>(filesShareService.insert(resources), HttpStatus.CREATED);
    }


    /**
     * 分享链接:通过被分享人id查询被分享的
     * @param userId
     * @param type
     * @param teamId
     * @param name
     * @param pageable
     * @return
     */
    @GetMapping("/sharedPerson")
//    @Log("分享链接:通过被分享人id查询被分享的")
//    @ApiOperation("分享链接:通过被分享人id查询被分享的")
    public ResponseEntity queryBySdPerson(@RequestParam(value = "userId",required = false) String userId,
                                          @RequestParam(value = "type") String type,
                                          @RequestParam(value = "teamId",required = false) String teamId,
                                          @RequestParam(value = "name",required = false) String name,
                                          Pageable pageable) {
        return new ResponseEntity<>(filesShareService.queryBySdPerson(userId,type,teamId,name, pageable), HttpStatus.OK);
    }


    /**
     * 分享链接:一键删除所有无效分享
     * @param userId
     * @param type
     * @param teamId
     * @return
     */
    @GetMapping("/delAllByType")
//    @Log("分享链接:一键删除所有无效分享")
//    @ApiOperation("分享链接:一键删除所有无效分享")
    public ResponseEntity delAllBySPerson(@RequestParam(value = "userId") String userId,
                                          @RequestParam(value = "type") String type,
                                          @RequestParam(value = "teamId",required = false) String teamId) {
        filesShareService.delAllBySPerson(userId,type,teamId);
        return new ResponseEntity<>(HttpStatus.OK);
    }




}
